<?php
namespace app\api\controller;

use think\Request;

class Jjcc extends Base
{   
    ##统计单日数据
    public function Statistics()
    {
        $day = date("Y-m-d");
        //新增用户
        $res = db('jjcc_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->find();
        if (!$res){
            //如果没有，则插入一条新增用户记录
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['user_new'] = 1;
            $data['create_time'] = $day;
            db('jjcc_statistics')->insert($data);
        }
 
        //登入用户
        $data =[];
        $data['admin_id'] = $this->accessData['aid'];
        $data['user_id'] = $this->accessData['uid'];
        $data['user_come'] = 1;
        $data['create_time'] = $day;
        $info = db('jjcc_statistics')->insert($data);
        
        
        //获取配置
        $info = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field('game_number_new,game_number_day')->find();
 
        //添加个人中心
        $arr = db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->find();
        if (!$arr){
            $data = [];
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['challenge_number'] = $info['game_number_new'];
            $data['create_time'] = date("Y-m-d");
            db('jjcc_personal')->insert($data);
        }else{
            if ($arr['create_time'] != $day){
                $data = [];
                $data['challenge_number'] = $arr['challenge_number'] + $info['game_number_day'];
                $data['create_time'] = date("Y-m-d");
                db('jjcc_personal')->where(['id'=>$arr['id']])->update($data);
            }
        }
        
        suc('ok');
    }
    
    ##首页文字//'https://sndzza.5iape.com'.'/'.$res['share_img']
    public function Index()
    {
        //获取获奖名单
        $where['a.admin_id'] = $this->accessData['aid'];
        $where['a.type'] = 2;
        $field = 'b.nickname,b.avatarurl';
        $res['prize'] = db('jjcc_challenge_log')->alias('a')->join('user b','a.user_id = b.id','left')
                        ->where($where)->field($field)->limit(20)->select();
        foreach ($res['prize'] as $k => $v){
            $res['prize'][$k]['title'] = "获得一个娃娃";
        }
        
        //其他游戏三款
        $field = "";
        $field = 'id,image,name,title,content,url';
        $where = [];
        $where['admin_id'] = $this->accessData['aid'];
        $where['sort'] = 1;
        $where['type'] = 3;
        $res['other'] = db('jjcc_game')->where($where)->field($field)->select();
        foreach ($res['other'] as $k => $v){
            $res['other'][$k]['image'] = $this->game_img($v['image']);
        }
                        
        //首页标题
        
        $field = "";
        $field = 'index_game,clearance,my_prize,game_rules,game_all,index_advertisement,prize_list,logo_title,comegame_title';
        $res['title'] = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field($field)->find();
        
        //更多游戏
        $field = "";
        $field = 'id,image,name,title,content,url';
        $where = [];
        $where['admin_id'] = $this->accessData['aid'];
        $where['sort'] = 1;
        $where['type'] = 0;
        $res['game'] = db('jjcc_game')->where($where)->field($field)->select();
        foreach ($res['game'] as $k => $v){
            $res['game'][$k]['image'] = $this->game_img($v['image']);
        }
        

        
        //首页广告
        $field = "";
        $field = 'index_advertisement_imgurl,index_advertisement_url';
        $res['advertisement'] = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field($field)->find();

        
        //奖品列表
        $field = "";
        $field = 'image,name';
        $res['gift'] = db('jjcc_prize')->where(['admin_id'=>$this->accessData['aid']])->field($field)->select();
        foreach ($res['gift'] as $k => $v){
            $res['gift'][$k]['image'] = $this->game_img($v['image']);
        }

        //分享标题+图片
        $field = "";
        $field = 'share_img,share_title';
        $res['img'] = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field($field)->find();
        $res['img']['share_img'] = $this->game_img($res['img']['share_img']);
        
        
        //首页右上角广告游戏
        $field = "";
        $field = "id,image,url";
        $where = [];
        $where['admin_id'] = $this->accessData['aid'];
        $where['sort'] = 1;
        $where['type'] = 1;
        $res['index_game'] = db('jjcc_game')->where($where)->field($field)->select();
        foreach ($res['index_game'] as $k => $v){
            $res['index_game'][$k]['image'] = $this->game_img($v['image']);
        }
        if (!$res){
            err('暂无数据');
        }else{
            suc('',$res);
        }
    }
    
    
    ##游戏规则
    public function Rules()
    {
        $res = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field('game_content')->find();
        
        if (!$res){
            err('暂无数据');
        }else{
            suc('',$res);
        }
    }
    
    
    ##我的奖品
    public function My_prize()
    {
        $where['a.admin_id'] = $this->accessData['aid'];
        $where['b.id'] = $this->accessData['uid'];
        $field = 'b.nickname,b.avatarurl,a.challenge_number,a.award_number,c.tel,c.go_prize';
        $res = db('jjcc_personal')
                ->alias('a')
                ->join('user b','a.user_id = b.id','left')
                ->join('jjcc_config c','a.admin_id = c.admin_id','left')
                ->field($field)->where($where)->find();
        
        $info = db('envelopes')
                ->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])
                ->field('money')
                ->find();
        if (!$info){
            $res['money'] = 0;
        }else{
            $res['money'] = $info['money'];
        }
                
        $where = [];
        $where['a.admin_id'] = $this->accessData['aid'];
        $where['a.user_id'] = $this->accessData['uid'];
        $field = '';
        $field = 'b.name,a.create_time,a.state,a.id';
        $res['my_prize'] = db('jjcc_address')
                            ->alias('a')
                            ->join('jjcc_prize b','a.prize_id = b.id','left')
                            ->where($where)
                            ->field($field)
                            ->select();
        
        //$res['my_money'] = db('')                   
        if ($res){
           suc('',$res); 
        }else{
            err('暂无数据');
        }
    }
    
    
    ##投诉信息列表
    public function Tscontent_list()
    {
        $res = db('jjcc_tscontent')->where(['admin_id'=>$this->accessData['aid']])->field('id,title')->select();
        
        if ($res){
            suc('',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##投诉提交
    public function Tscontent_post()
    {
        $post = request()->only(['id'],'post');
        
        if (empty($post['id'])){
            err('投诉ID不能为空');
        }
        
        $data['admin_id'] = $this->accessData['aid'];
        $data['user_id'] = $this->accessData['uid'];
        $data['conetnt'] = $post['id'];
        $data['create_time'] = date("Y-m-d H:i:s");
        
        $res = db('jjcc_complaint')->insert($data);
        
        if ($res){
            suc('投诉成功');
        }else{
            err('投诉失败');
        }
    }
    
    
    ##去领奖页面
    public function Go_prize()
    {
        //联系电话
        $res['tel'] = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field('tel')->find();
        
        //选择奖品
        $res['prize'] = db('jjcc_prize')->where(['admin_id'=>$this->accessData['aid']])->field('id,image,name')->select();
        
        foreach ($res['prize'] as $k => $v){
            $res['prize'][$k]['image'] = $this->game_img($v['image']);
        }

        if ($res){
            suc('',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##去领奖提交
    public function Go_prize_post()
    {
        $post = request()->only(['prize_id','name','tel','address','content'],'post');

        
        if (empty($post['prize_id'])){
            err('礼物ID不能为空');
        }
        
        if (empty($post['name'])){
            err('收货人不能为空');
        }
        
        if (empty($post['tel'])){
            err('收货人电话为空');
        }
        
        if (empty($post['address'])){
            err('收货人地址不能为空');
        }
        
        if (empty($post['content'])){
            $post['content'] = "这个人很懒，什么都没留下";
        }
        
        $post['admin_id'] = $this->accessData['aid'];
        $post['user_id'] = $this->accessData['uid'];

        $post['create_time'] = date("Y-m-d");
        
        $res = db('jjcc_address')->insert($post);
        
        $arr = db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->field('award_number')->find();
        
        if ($arr){
            //更新领取娃娃次数
            $data = [];
            $data['award_number'] = $arr['award_number'] + 1;
            db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->update($data);
        }
        
        
        if ($res){
            suc('领取成功');
        }else{
            err('领取失败');
        }
    }
    
    
    ##开始游戏
    public function Come_game()
    {
        //判断挑战次数
        $where['admin_id'] = $this->accessData['aid'];
        $where['user_id'] = $this->accessData['uid'];
        $res = db('jjcc_personal')->where($where)->field('challenge_number')->find();
        
        //没次数分享标题
        
        $title = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field('share_humber')->find();
        if ($res['challenge_number'] == 0){
            err($title['share_humber']);
        }else{
            //挑战次数-1
            db('jjcc_personal')->where($where)->setDec('challenge_number');
            
            //增加参与次数
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['user_number'] = 1;
            $data['create_time'] = date("Y-m-d");
            db('jjcc_statistics')->insert($data);
            
            suc('OK');
        }
    }
    
    
    ##开始成功进入返题
    public function Problem()
    {
        //获取关卡配置
        $info = db('jjcc_config')
                ->where(['admin_id'=>$this->accessData['aid']])
                ->field('game_a,count_down_a,count_down_reduce_a,
                        game_b,count_down_b,count_down_reduce_b,
                        game_c,count_down_c,count_down_reduce_c,
                        game_d,count_down_d,count_down_reduce_d,checkpoint_number')
                ->find();
        

        //难度一
        $resa = db('jjcc_subject')->where(['admin_id'=>$this->accessData['aid'],'type'=>1])->order('rand()')->limit($info['game_a'])->select();
        
        foreach ($resa as $key => $val){
            if ($key == 0){
                $i = 0;
            }
            $i = $info['count_down_reduce_a'] * $key;

            $resa[$key]['game_time'] = $info['count_down_a'] - $i;
        }
        
        //难度二
        $resb = db('jjcc_subject')->where(['admin_id'=>$this->accessData['aid'],'type'=>2])->order('rand()')->limit($info['game_b'])->select();
        
        foreach ($resb as $key => $val){
            if ($key == 0){
                $i = 0;
            }
            $i = $info['count_down_reduce_b'] * $key;
        
            $resb[$key]['game_time'] = $info['count_down_b'] - $i;
        }
        
        //难度三
        $resc = db('jjcc_subject')->where(['admin_id'=>$this->accessData['aid'],'type'=>3])->order('rand()')->limit($info['game_c'])->select();
        
        foreach ($resc as $key => $val){
            if ($key == 0){
                $i = 0;
            }
            $i = $info['count_down_reduce_c'] * $key;
        
            $resc[$key]['game_time'] = $info['count_down_c'] - $i;
        }
        
        //难度四
        $resd = db('jjcc_subject')->where(['admin_id'=>$this->accessData['aid'],'type'=>4])->order('rand()')->limit($info['game_d'])->select();
        
        foreach ($resd as $key => $val){
            if ($key == 0){
                $i = 0;
            }
            $i = $info['count_down_reduce_d'] * $key;
        
            $resd[$key]['game_time'] = $info['count_down_d'] - $i;
        }
        
        $arr['answer'] = array_merge($resa,$resb,$resc,$resd);
        $arr['game_number'] = $info['checkpoint_number'];
        
        
        if ($arr){
            suc('',$arr);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##提交答案
    public function Answer_post()
    {
        $post = request()->only(['type','game_number'],'post');
        
        if (empty($post['type'])){
            err('答题类型不能为空');
        }
            
        if (empty($post['type'])){
            err('关卡数不能为空');
        }
        
        //获取挑战文本
        $info = db('jjcc_config')->where(['admin_id'=>$this->accessData['aid']])->field('game_ok_one,wrong_one,wrong_two,game_ok_title,go_prize')->find();
        
        $data['admin_id'] = $this->accessData['aid'];
        $data['user_id'] = $this->accessData['uid'];
        $data['type'] = $post['type'];
        $data['score'] = $post['game_number'];
        $data['create_time'] = date("Y-m-d H:i:s");
        

        if ($post['type'] == 3){
            $title = "中途离开";
            $res = db('jjcc_challenge_log')->insert($data); 
        }elseif ($post['type'] == 2){
            $title = ['name'=>$info['game_ok_title'],'title'=>$info['game_ok_one'],'go_prize'=>$info['go_prize']];
            $res = db('jjcc_challenge_log')->insert($data);
        }elseif ($post['type'] == 1){
            $title = ['name'=>$info['wrong_one'],'title'=>$info['wrong_two']];
            $res = db('jjcc_challenge_log')->insert($data);
        }
        
        if (!$res){
            err('参数错误');
        }else{
            suc($title);
        } 
    }
    
    
    ##分享求助
    public function Seek_help()
    {
        $apd = "wxe106ead94e22792c";
        $post = request()->only(['type','encryptedData','vi'],'post');
        if (!empty($post['encryptedData'])){
            $encryptedData = $post['encryptedData'];
        }
        if (!empty($post['vi'])){
            $vi = $post['vi'];
        }

        //获得分享标语
		$list_info = db('jjcc_config')
					->field('share_title,share_day,share_daypu')
					->find();
         
        $day = date("Y-m-d");
        $list = db('user')->field('session_key')->where(['admin_id'=>$this->accessData['aid'],'id'=>$this->accessData['uid']])->find();
        //分享到群
        if ($post['type'] == 2){
            $memberdecrypt = $this->getDecrypt($list['session_key'],$encryptedData,$vi,$apd);
            
            if($memberdecrypt['openGId']){
                $group_id = $memberdecrypt['openGId'];
            }
            //获取当前群分享次数
            $res_info = db('jjcc_statistics')
                        ->where([
                                'admin_id'=>$this->accessData['aid'],
                                'user_id'=>$this->accessData['uid'],
                                'create_time'=>$day,
                                'game_id'=>$group_id,
                                'type'=>2])
                        ->count('id');

            if ($res_info >= $list_info['share_day']){
                
                //增加分享次数
                db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['share_number'] = 1;
                $dat['type'] = 2;
                $dat['game_id'] = $group_id;    
                $dat['create_time'] = date("Y-m-d");
                db('jjcc_statistics')->insert($dat);
                
                err('您今天分享当前群次数过多');
            }
            

            //增加分享记录
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['share_number'] = 1;
            $data['type'] = 2;
            $data['game_id'] = $group_id;
            $data['create_time'] = date("Y-m-d");
            
            $res = db('jjcc_statistics')->insert($data);
            
            if($res){
            
                //增加分享次数
                db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                //增加挑战机会
                db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('challenge_number');
                suc('分享成功');
            }
        
        //分享到个人    
        }elseif($post['type'] == 1){
            //获取分享次数
            $res_info = db('jjcc_statistics')
                        ->where([
                                'admin_id'=>$this->accessData['aid'],
                                'user_id'=>$this->accessData['uid'],
                                'create_time'=>$day,
                                'type'=>1])
                        ->count('id');

            if ($res_info >= $list_info['share_daypu']){
                

                //增加分享次数
                db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['share_number'] = 1;
                $dat['create_time'] = date("Y-m-d");
                db('jjcc_statistics')->insert($dat);
                
                err('您今天分享此好友已上限');
            }
            
            
            
            //增加分享记录
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['share_number'] = 1;
            $data['type'] = 1;
            $data['create_time'] = date("Y-m-d");
            
            $res = db('jjcc_statistics')->insert($data);
            
            if($res){
                //增加分享次数
                db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                //增加挑战机会
                db('jjcc_personal')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('challenge_number');
                suc('分享成功');
            }
        }
        
    }
    
    
    ##更多游戏跳转统计
    public function All_Game()
    {
        $post = request()->only(['game_id'],'post');
        
        if (empty($post['game_id'])){
            err('游戏ID不能为空');
        }
        
        $data['admin_id'] = $this->accessData['aid'];
        $data['user_id'] = $this->accessData['uid'];
        $data['game_id'] = $post['game_id'];
        $data['create_time'] = date("Y-m-d");
        $res = db('jjcc_game_log')->insert($data);
        
        suc('OK');
    }
    
    
    /**
     * 红包接口
     */
    ##点击开始随机获取金额
    public function money()
    {
        $res = db('jjcc_config')->field('money_min,money_max')->find();
        
        $money['money'] = $res['money_min'] + mt_rand() / mt_getrandmax() * ($res['money_max'] - $res['money_min']);
        $money['money'] =  sprintf("%.2f",$money['money']);
        
        
        if($money){
            $where['admin_id'] = $this->accessData['aid'];
            $where['user_id'] = $this->accessData['uid'];
            //获取原来金额
            $arr = db('envelopes')->where($where)->field('money')->find();
            if (!$arr){
                $str['admin_id'] = $this->accessData['aid'];
                $str['user_id'] = $this->accessData['uid'];
                $str['create_time'] = date("Y-m-d H;i:s");
                $str['money'] = $money['money'];
                db('envelopes')->insert($str);
            }else{
                //增加余额
                $data['money'] = $arr['money'] + $money['money'];
                $data['create_time'] = date("Y-m-d H;i:s");
                db('envelopes')->where($where)->update($data);
            }
            
            //增加获得红包记录
            $info['admin_id'] = $this->accessData['aid'];
            $info['user_id'] = $this->accessData['uid'];
            $info['money'] = $money['money'];
            $info['type'] = 1;
            $info['create_time'] = date("Y-m-d H:i:s");
            db('envelopes_water')->insert($info);
            suc('',$money); 
        }else{
            err('系统出错');
        }
    }
    
    
    ##立即提现余额页面
    public function money_list()
    {
        $where['admin_id'] = $this->accessData['aid'];
        $where['user_id'] = $this->accessData['uid'];
        
        $info = db('jjcc_config')->field('money_all')->find();
        
        $res = db('envelopes')->where($where)->field('money')->find();
        
        $res['money_all'] = $info['money_all'];
        if ($res){
            suc('',$res);
        }else{
            err('无数据');
        }
    }
    
    ##立即提现提交
    public function money_post()
    {
        $post = request()->only(['money'],'post');
        
        $info = db('jjcc_config')->field('money_all,customer_service')->find();
        
        if (empty($post['money'])){
            err('金额不能为空');
        }
        
        $where['admin_id'] = $this->accessData['aid'];
        $where['user_id'] = $this->accessData['uid'];
        $res = db('envelopes')->where($where)->field('money')->find();
        
        if ($post['money'] > $res['money']){
            err('余额不足');
        }elseif($post['money'] < $info['money_all']){
            err('不足提现金额');
        }
        
        
        //增加提现记录
        $data['admin_id'] = $this->accessData['aid'];
        $data['user_id'] = $this->accessData['uid'];
        $data['create_time'] = date("Y-m-d H;i:s");
        $data['type'] = 2;
        $data['money'] = $post['money'];
        $arr = db('envelopes_water')->insert($data);

        if ($arr){
            //余额减去
            $str['money'] = $res['money'] - $post['money'];
            db('envelopes')->where($where)->update($str);
            
            //客服微信
            
            suc('成功',$info);
        }else{
            err('失败');
        }
        
        
    }
    
    ##余额变动记录
    public function money_edit()
    {
        $where['admin_id'] = $this->accessData['aid'];
        $where['user_id'] = $this->accessData['uid'];
        $where['type'] = 2;
        $res = db('envelopes_water')->where($where)->field('money,create_time')->select();
        
        if ($res){
            suc('',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    ##红包获得记录
    public function money_get()
    {
        $where['admin_id'] = $this->accessData['aid'];
        $where['user_id'] = $this->accessData['uid'];
        $where['type'] = 1;
        $res = db('envelopes_water')->where($where)->field('money,create_time')->select();
    
        if ($res){
            suc('',$res);
        }else{
            err('暂无数据');
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}